package com.cdbkj.cloud.pos.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cdbkj.cloud.pos.R;
import com.cdbkj.cloud.pos.utils.CustomPwdDialog;
import com.cdbkj.cloud.pos.utils.Tools;
import com.dawn.cloud.pos.drive.sdk.YBSdk;
import com.orhanobut.logger.Logger;

public class MedicalCardFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private TextView temTV;
    private Runnable runnable;
    protected int count = 0;

    private OnMedicalCardFragmentInteractionListener mListener;

    public MedicalCardFragment() {
        // Required empty public constructor
    }

    public static MedicalCardFragment newInstance(String param1, String param2) {
        MedicalCardFragment fragment = new MedicalCardFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        temTV = new TextView(getActivity());
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.dismissDialog();
                getMedicalCardInfo();
            }
        };
        Tools.showDialog("请确认医保卡已经插入设备读卡器！", getActivity(), clickListener, clickListener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_medical_card, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnMedicalCardFragmentInteractionListener) {
            mListener = (OnMedicalCardFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnMedicalCardFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnMedicalCardFragmentInteractionListener {
        void onMedicalCardFragmentInteraction(String pwd, FragmentActivity activity);
    }

    public void getMedicalCardInfo() {
        View.OnClickListener clickListener = new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //3、获取密码
              String  pwd = Tools.getPwdDialogPwd();
              if(pwd.length()==6){
                  mListener.onMedicalCardFragmentInteraction(pwd,getActivity());
              }
            }
        };
        Tools.showPwdDialog(getActivity(), clickListener);


            /*   runnable = new Runnable() {
                        @Override
                        public void run() {
                            //如果获取到医保卡信息
                            count++;
                            if (count > 10) {
                                temTV.removeCallbacks(runnable);
                                Tools.showDialogWithReturnMainActivity("交易无法进行", getActivity());
                                return;
                }
                //1、停止进程；
                temTV.removeCallbacks(runnable);
                //2、弹出密码框；
              *//*  View.OnClickListener clickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //3、获取密码
                        String pwd = Tools.getPwdDialogPwd();
                        //4、调用医保支付服务；

                    }
                };
                Tools.showPwdDialog(getActivity(), clickListener);*//*
                //每1秒执行一次*//*
                temTV.postDelayed(this, 10000);
            }
        };*/
     //   temTV.postDelayed(runnable, 1000);
    }
}
